﻿using LCL.Config;
using LCL.LData;
using LCL.RedisCache.Config;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Reflection;

namespace LCL.Tests.Common
{
    public static class Helper
    {
        #region Constants

        public const string EF_SQL_ConnectionString = @"Server=.\SQLEXPRESS; Database=EFTestContext; Integrated Security=True; MultipleActiveResultSets=True;";

        #endregion

        #region Instrumentation

        private static string CreateMessageQueueName(string mq)
        {
            return string.Format(@".\private$\{0}", mq);
        }

        public static void WriteTempFile(string content)
        {
            string tempPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
            string fileName = Path.Combine(tempPath, "lcl.txt");
            File.WriteAllText(fileName, content);
        }

        public static bool IsTempFileExists()
        {
            string tempPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
            string fileName = Path.Combine(tempPath, "lcl.txt");
            return File.Exists(fileName);
        }

        public static string ReadTempFile()
        {
            string tempPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
            string fileName = Path.Combine(tempPath, "lcl.txt");
            return File.ReadAllText(fileName);
        }

        public static void DeleteTempFile()
        {
            string tempPath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
            string fileName = Path.Combine(tempPath, "lcl.txt");
            File.Delete(fileName);
        }
        #endregion

        #region Config
        public static DbConfigInfo GetDbConfig()
        {
            DbConfigInfo config = DbConfig.GetConfig();
            if (config == null)
            {
                config = new DbConfigInfo();
                config.DbHost = "localhost";
                config.DbType = LDbType.MySQL;
                config.DbUser = "root";
                config.DbPwd = "123456";
                DbConfig.SaveConfig(config);
            }
            return config;
        }
        public static List<BusConfigInfo> GetBugConfig()
        {
            List<BusConfigInfo> list = BusConfigs.Configs;
            if (list == null || list.Count == 0)
            {
                list = new List<BusConfigInfo>();
                BusConfigInfo info = new BusConfigInfo();
                info.PublishEvent = new SubscribedEvent { EventAssembly = "LCL.Tests.Common", EventType = "LCL.Tests.Common.Events.UserGeneratorEvent" };
                info.SubscribedEvents = new List<SubscribedEvent>()
                {
                     new SubscribedEvent { EventAssembly = "LCL.Tests.Common", EventType = "LCL.Tests.Common.Events.UserAddedEventHandlerSendEmail" },
                     new SubscribedEvent { EventAssembly = "LCL.Tests.Common", EventType = "LCL.Tests.Common.Events.UserAddedEventHandlerSendMessage" },
                     new SubscribedEvent { EventAssembly = "LCL.Tests.Common", EventType = "LCL.Tests.Common.Events.UserAddedEventHandlerSendRedbags" }
                };

                BusConfigInfo info1 = new BusConfigInfo();
                info.PublishEvent = new SubscribedEvent { EventAssembly = "LCL.Tests.Common", EventType = "LCL.Tests.Common.Events.OrderGeneratorEvent" };
                info.SubscribedEvents = new List<SubscribedEvent>()
                {
                     new SubscribedEvent { EventAssembly = "LCL.Tests.Common", EventType = "LCL.Tests.Common.Events.UserAddedEventHandlerSendRedbags" }
                };

                list.Add(info);
                list.Add(info1);

                BusConfigs.Save(list);
            }
            return list;
        }
        public static LEmailConfigInfo GetEmailConfig()
        {
            LEmailConfigInfo model = LEmailConfigs.GetConfig();
            if (model == null)
            {
                model = new LEmailConfigInfo();
                model.SendEmail = "minguiluo@163.com";
                model.SendPwd = "4485083";//密码
                model.SendSetSmtp = "smtp.163.com";//发送的SMTP服务地址 ，每个邮箱的是不一样的。。根据发件人的邮箱来定
                model.ConsigneeAddress = "271391233@qq.com";
                model.ConsigneeHand = "271391233@qq.com";
                model.ConsigneeTheme = "主题";
                model.ConsigneeName = "";
                model.SendContent = "";
                LEmailConfigs.SaveConfig(model);
            }
            return model;
        }
        public static FTPConfigInfo GetFTPConfig()
        {
            List<FTPConfigInfo> configs = FTPConfigs.GetConfig;
            if (configs == null || configs.Count == 0)
            {
                configs = new List<FTPConfigInfo>();

                var config = new FTPConfigInfo();
                config.Server = "127.0.0.1";
                config.Port = 21;
                config.User = "6396000644";
                config.Pass = "lmg*123456";

                configs.Add(config);
                FTPConfigs.SaveConfig(configs);
            }
            return configs[0];
        }
        public static RedisConfigInfo GetRedisConfig()
        {
            var config = RedisConfigs.GetConfig();
            if (config == null)
            {
                config = new RedisConfigInfo();
                config.ReadServerList = "127.0.0.1:6379";
                config.WriteServerList = "127.0.0.1:6379";
                config.ApplyRedis = true;
                RedisConfigs.SaveConfig(config);
            }
            return config;
        }
        #endregion

        /// <summary>
        /// 向指定的邮件地址发送邮件。
        /// </summary>
        /// <param name="to">需要发送邮件的邮件地址。</param>
        /// <param name="subject">邮件主题。</param>
        /// <param name="content">邮件内容。</param>
        public static void SendEmail(string to, string subject, string content)
        {
            //  <emailClient host="smtp.163.com" port="25" userName="byteartretail" password="byteartretail123" enableSsl="false" sender="byteartretail@163.com" />
            MailMessage msg = new MailMessage("minguiluo@163.com", to, subject, content);
            SmtpClient smtpClient = new SmtpClient("smtp.163.com");
            smtpClient.Port = 25;
            smtpClient.Credentials = new NetworkCredential("minguiluo@163.com", "4485083");
            smtpClient.EnableSsl = false;
            smtpClient.Send(msg);
        }
    }
}